package com.hdw.med.controller;

import com.hdw.common.base.BaseException;
import com.hdw.common.base.BaseResult;
import com.hdw.common.constant.CommonResultConstant;
import com.hdw.common.vo.LayMsg;
import com.hdw.common.vo.PaginationVO;
import com.hdw.med.api.BaseDbTpcompService;
import com.hdw.med.bean.model.BaseDbTpcomp;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * baseDbTpcomp控制器
 * Created by hozeData on 2021/05/28.
 */
@RestController
@RequestMapping("/etl/baseDbTpcomp/")
@CrossOrigin(origins = "*", maxAge = 3600)
@Api(tags = "数据库字段类型")
public class BaseDbTpcompController {

    @Autowired
    BaseDbTpcompService baseDbTpcompService;

    @ApiOperation(value = "分页查询")
    @RequestMapping(value = "findByPage", method = RequestMethod.POST)
    public BaseResult findByPage(@RequestBody BaseDbTpcomp baseDbTpcomp, Integer pageNum, Integer pageSize) {
        PaginationVO<BaseDbTpcomp> pageVO = baseDbTpcompService.findPageByModel(baseDbTpcomp, pageNum, pageSize);
        return new BaseResult(CommonResultConstant.SUCCESS, pageVO);
    }


    @ApiOperation(value = "获取JAVA类型")
    @RequestMapping(value = "findJavaType", method = RequestMethod.GET)
    public LayMsg findJavaType() {
        List<BaseDbTpcomp> byModel = baseDbTpcompService.findByModel(new BaseDbTpcomp());
        Map<String, Map<String, String>> result = new HashMap<>();
        for (BaseDbTpcomp baseDbTpcomp : byModel) {
            Map<String, String> one = new HashMap<>();
            one.put("cd", baseDbTpcomp.getEuJavatp().toString());
            one.put("na", baseDbTpcomp.getNaJavatp());
            result.put(baseDbTpcomp.getEuJavatp().toString(), one);
        }

        return LayMsg.success().addData(result);
    }

    @ApiOperation(value = "查询")
    @RequestMapping(value = "find", method = RequestMethod.GET)
    public LayMsg find(String dbType, Integer page, Integer limit) {
        BaseDbTpcomp baseDbTpcomp = new BaseDbTpcomp();
        baseDbTpcomp.setEuDbtp(dbType);
        List<BaseDbTpcomp> pageVO = baseDbTpcompService.findByModel(baseDbTpcomp);
        return LayMsg.success().addCount(pageVO.size()).addData(pageVO);
    }

    @ApiOperation(value = "保存")
    @ApiImplicitParam(name = "baseDbTpcomp", value = "实体", paramType = "body", required = true, dataType = "baseDbTpcomp")
    @RequestMapping(value = "save", method = RequestMethod.POST)
    public LayMsg save(@RequestBody BaseDbTpcomp baseDbTpcomp, HttpServletRequest request) {
        int num;
        if (baseDbTpcomp.getIdDbTpcomp() == null) {
            num = baseDbTpcompService.insertSelective(baseDbTpcomp);
        } else {
            num = baseDbTpcompService.updateByIdSelective(baseDbTpcomp);
        }
        return LayMsg.success();
    }

    @ApiOperation(value = "详情")
    @ApiImplicitParam(name = "idDbTpcomp", value = "主键", paramType = "query", required = true, dataType = "Long")
    @RequestMapping(value = "getById", method = RequestMethod.GET)
    public BaseResult getById(Long idDbTpcomp, HttpServletRequest request) throws BaseException {
        if (StringUtils.isEmpty(idDbTpcomp)) {
            throw new BaseException("请选择要编辑的项");
        }
        BaseDbTpcomp baseDbTpcomp = baseDbTpcompService.getById(idDbTpcomp);
        return new BaseResult(CommonResultConstant.SUCCESS, baseDbTpcomp);
    }

    @ApiOperation(value = "根据主键删除")
    @ApiImplicitParam(name = "idDbTpcomp", value = "主键", paramType = "query", required = true, dataType = "Long")
    @RequestMapping(value = "delete", method = RequestMethod.GET)
    public BaseResult delete(Long idDbTpcomp, HttpServletRequest request) throws BaseException {
        if (StringUtils.isEmpty(idDbTpcomp)) {
            throw new BaseException("请选择要删除的项");
        }
        int num = baseDbTpcompService.deleteById(idDbTpcomp);
        return new BaseResult(CommonResultConstant.SUCCESS, num);
    }


}
